;;........................................................................ 
;; Easy Aerosol diag
;;
;; Clear-sky shortwave flux at TOA
;; Clear-sky shortwave flux at surface 
;; Clear-sky shortwave flux in atmosphere
;;........................................................................ 
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"   
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"   
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"   

begin

  hres = "T31"
  yy = "1977"
  tp = "amip"

  hres = getenv("hres")
  yy = getenv("yy")
  tp = getenv("tp")

  xx = "_"

  plotname = "./figure/figure_allsky_zonal" + xx + yy + xx + hres + xx + tp 

  myfont = 0.020

  if(tp.eq."amip") then
     cs1 = "EXP04"
     cs2 = "EXP05"
  else
     cs1 = "EXP01"
     cs2 = "EXP02"
  end if

  fna = cs1+"_"+hres+xx+yy+".nc"
  fnb = cs2+"_"+hres+xx+yy+".nc"
  fnc = cs2+"_"+hres+xx+yy+".nc"

  print("read file : "+fna)
  print("read file : "+fnb)
  print("read file : "+fnc)

  system("ls -l "+fna)
  system("ls -l "+fnb)
  system("ls -l "+fnc)

  fla = addfile(fna,"r") 
  flb = addfile(fnb,"r") 
  flc = addfile(fnc,"r") 

  lat  = fla->lat
  lon  = fla->lon
  time = fla->time

  ;; TOA 

  atoa = fla->FSNT
  btoa = flb->FSNT
  ctoa = flc->FSNT

  ptoa = atoa
  ptoa = ctoa - atoa

  ;; Surface 

  asfc = fla->FSNS
  bsfc = flb->FSNS
  csfc = flc->FSNS

  psfc = asfc
  psfc = csfc - asfc

  ;; Atmosphere

  aatm = atoa 
  batm = atoa 
  catm = atoa 

  aatm = atoa - asfc 
  batm = btoa - bsfc 
  catm = ctoa - csfc 

  patm = aatm
  patm = catm - aatm

  nlon = dimsizes(lon) 
  nlat = dimsizes(lat) 

  ;;................................
  ;; plotting 
  ;;................................

  plot    = new ( 3, "graphic")

  wks  = gsn_open_wks ("pdf",plotname) 

  colormap = "ViBlGrWhYeOrRe"
  colormap = "testcmap"
  colormap = "amwg"
  colormap = "BlueYellowRed" 

  gsn_define_colormap(wks,colormap)         ; choose a colormap


  res  = True
  res@gsnDraw                = False
  res@gsnFrame               = False
  ;;res@tiYAxisString          = "PDF (%)"
  ;;res@tiXAxisString          = "10m wind speed (m s~S~-1~N~)"

  res@tiMainOn         = False   ; no title
  res@cnFillOn         = True    ; turn on color
  res@cnLinesOn        = False   ; turn off contour lines
  res@cnLineThicknessF = 1.     ; thicker lines
  ;;res@cnLevelSelectionMode  = "ManualLevels"  ; set manual cn levels
  ;;res@cnMinLevelValF   =    0.     ; set min contour level
  ;;res@cnMaxLevelValF   = 2.     ; set max contour level
  ;;res@cnLevelSpacingF  = 0.1
  res@cnInfoLabelOn    = False    ; no contour labels
;;  res@cnFillMode       = "RasterFill"  ; turn on raster mode
 ;res@cnFillDrawOrder  = "PreDraw"     ; draw contours before continents

  res@gsnSpreadColors = True    ; use full colormap



;;----------------------------------------------------------------------
;; lable bar
;;----------------------------------------------------------------------

  res@lbLabelBarOn       = True       ; no individual label bar
  res@lbLabelStride      = 1          ; every other label bar label
  res@lbOrientation      = "Vertical" ;;Horizontal" ; vertical label bar
  res@lbLabelFontHeightF = myfont * 0.8
  res@pmLabelBarWidthF   = 0.08
  res@pmLabelBarHeightF  = 0.4
  ;;res@pmLabelBarOrthogonalPosF =  0.18


  ;;colors = (/"white","black","White","RoyalBlue","LightSkyBlue",\
  ;;            "PowderBlue","lightseagreen","PaleGreen","Wheat","Brown",\
  ;;            "Pink","darkgreen","darkorange","red","grey"/)

  ;;;;res@xyLineThicknessF       = 2
  ;;res@xyLineThicknesses = 4. + 0.*fspan(0,10,10)
  ;;;;res@xyDashPatterns    = (/0.,1.,2.,0.,1.,2./) ;;0.*fspan(0,10,10) 
  ;;res@xyDashPatterns    = (/0.,1.,0.,1./) ;;0.*fspan(0,10,10) 
  ;;;;res@xyLineColors      = (/"RoyalBlue","grey","Wheat","blue","red","darkgreen","darkorange","brown","lightseagreen","black"/)    ; change line color
  ;;;;res@xyLineColors      = (/"blue","blue","green","red","red","orange"/) 
  ;;res@xyLineColors      = (/"black","red","red","red"/) 

  res@gsnStringFontHeightF     = myfont
  res@tmXBLabelFontHeightF = myfont * 0.8 
  res@tmYLLabelFontHeightF = myfont* 0.8 

  res@tiXAxisFontHeightF = myfont
  res@tiYAxisFontHeightF = myfont

  res@cnLevelSelectionMode  = "ExplicitLevels"  ; set manual cn levels
  res@cnLevels = (/-8,-6,-4,-2,-1,0,1,2,4,6,8/) 

  res@gsnLeftString = "a) TOA all-sky SW flux perturbation" 
  res@gsnRightString = "W m~S~-2~"
  res@gsnCenterString        = "" 
  plot(0) = gsn_csm_contour_map_ce(wks, ptoa(0,:,:), res)

  res@gsnLeftString = "b) Surface all-sky SW flux perturbation" 
  res@gsnRightString = "W m~S~-2~"
  res@gsnCenterString        = "" 
  plot(1) = gsn_csm_contour_map_ce(wks, psfc(0,:,:), res)

  ;;delete(res@cnLevels) 
  res@gsnLeftString = "c) Atmosphere all-sky SW flux perturbation" 
  res@gsnRightString = "W m~S~-2~"
  res@gsnCenterString        = "" 
  plot(2) = gsn_csm_contour_map_ce(wks, patm(0,:,:), res)


  resP    = True
  resP@txString  = ""
  resP@gsnPanelYWhiteSpacePercent = 5
  resP@gsnPanelXWhiteSpacePercent = 5
  gsn_panel(wks,plot,(/3,1/),resP)


end







